home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / XML / Transformer / CallbackRegistry.php next >
PHP Script  |  2004-03-24  |  7KB  |  238 lines

  1. <?php
  2. //
  3. // +---------------------------------------------------------------------------+
  4. // | PEAR :: XML :: Transformer                                                |
  5. // +---------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
  7. // |                         Kristian K÷hntopp <kris@koehntopp.de>.            |
  8. // +---------------------------------------------------------------------------+
  9. // | This source file is subject to version 3.00 of the PHP License,           |
  10. // | that is available at http://www.php.net/license/3_0.txt.                  |
  11. // | If you did not receive a copy of the PHP license and are unable to        |
  12. // | obtain it through the world-wide-web, please send a note to               |
  13. // | license@php.net so we can mail you a copy immediately.                    |
  14. // +---------------------------------------------------------------------------+
  15. //
  16. // $Id: CallbackRegistry.php,v 1.18 2004/01/01 10:31:53 sebastian Exp $
  17. //
  18.  
  19. /**
  20. * Callback Registry.
  21. *
  22. * @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
  23. * @author  Kristian K÷hntopp <kris@koehntopp.de>
  24. * @version $Revision: 1.18 $
  25. * @access  public
  26. */
  27. class XML_Transformer_CallbackRegistry {
  28.     // {{{ Members
  29.  
  30.     /**
  31.     * @var    array
  32.     * @access public
  33.     */
  34.     var $overloadedNamespaces = array();
  35.  
  36.     /**
  37.     * @var    boolean
  38.     * @access private
  39.     */
  40.     var $_locked = false;
  41.  
  42.     /**
  43.     * If true, the transformation will continue recursively
  44.     * until the XML contains no more overloaded elements.
  45.     * Can be overrided on a per-element basis.
  46.     *
  47.     * @var    boolean
  48.     * @access private
  49.     */
  50.     var $_recursiveOperation = true;
  51.  
  52.     // }}}
  53.     // {{{ function XML_Transformer_CallbackRegistry($recursiveOperation)
  54.  
  55.     /**
  56.     * Constructor.
  57.     *
  58.     * @param  boolean
  59.     * @access public
  60.     */
  61.     function XML_Transformer_CallbackRegistry($recursiveOperation) {
  62.         $this->_recursiveOperation = $recursiveOperation;
  63.     }
  64.  
  65.     // }}}
  66.     // {{{ function &getInstance($recursiveOperation)
  67.  
  68.     /**
  69.     * Returns an instance of XML_Transformer_CallbackRegistry.
  70.     *
  71.     * @param  boolean
  72.     * @return XML_Transformer_CallbackRegistry
  73.     * @access public
  74.     */
  75.     function &getInstance($recursiveOperation) {
  76.         static $instance;
  77.  
  78.         if (!isset($instance)) {
  79.             $instance = new XML_Transformer_CallbackRegistry($recursiveOperation);
  80.         }
  81.  
  82.         return $instance;
  83.     }
  84.  
  85.     // }}}
  86.     // {{{ function overloadNamespace($namespacePrefix, &$object, $recursiveOperation = '')
  87.  
  88.     /**
  89.     * Overloads an XML Namespace.
  90.     *
  91.     * @param  string
  92.     * @param  object
  93.     * @param  boolean
  94.     * @return mixed
  95.     * @access public
  96.     */
  97.     function overloadNamespace($namespacePrefix, &$object, $recursiveOperation = '') {
  98.         if (!is_object($object)) {
  99.             return sprintf(
  100.               'Cannot overload namespace "%s", ' .
  101.               'second parameter is not an object.',
  102.  
  103.               $namespacePrefix
  104.             );
  105.         }
  106.  
  107.         if (!is_subclass_of($object, 'XML_Transformer_Namespace')) {
  108.             return sprintf(
  109.               'Cannot overload namespace "%s", ' .
  110.               'provided object was not instantiated from ' .
  111.               'a class that inherits XML_Transformer_Namespace.',
  112.  
  113.               $namespacePrefix
  114.             );
  115.         }
  116.  
  117.         if (!method_exists($object, 'startElement') ||
  118.             !method_exists($object, 'endElement')) {
  119.             return sprintf(
  120.               'Cannot overload namespace "%s", ' .
  121.               'method(s) "startElement" and/or "endElement" ' .
  122.               'are missing on given object.',
  123.  
  124.               $namespacePrefix
  125.             );
  126.         }
  127.  
  128.         $this->overloadedNamespaces[$namespacePrefix]['active']             = true;
  129.         $this->overloadedNamespaces[$namespacePrefix]['object']             = &$object;
  130.         $this->overloadedNamespaces[$namespacePrefix]['recursiveOperation'] = is_bool($recursiveOperation) ? $recursiveOperation : $this->_recursiveOperation;
  131.  
  132.         return true;
  133.     }
  134.  
  135.     // }}}
  136.     // {{{ function unOverloadNamespace($namespacePrefix)
  137.  
  138.     /**
  139.     * Reverts overloading of a given XML Namespace.
  140.     *
  141.     * @param  string
  142.     * @access public
  143.     */
  144.     function unOverloadNamespace($namespacePrefix) {
  145.         if (isset($this->overloadedNamespaces[$namespacePrefix])) {
  146.             unset($this->overloadedNamespaces[$namespacePrefix]);
  147.         }
  148.     }
  149.  
  150.     // }}}
  151.     // {{{ function isOverloadedNamespace($namespacePrefix)
  152.  
  153.     /**
  154.     * Returns true if a given namespace is overloaded,
  155.     * false otherwise.
  156.     *
  157.     * @param  string
  158.     * @return boolean
  159.     * @access public
  160.     */
  161.     function isOverloadedNamespace($namespacePrefix) {
  162.         return isset(
  163.           $this->overloadedNamespaces[$namespacePrefix]
  164.         );
  165.     }
  166.  
  167.     // }}}
  168.     // {{{ function setRecursiveOperation($recursiveOperation)
  169.  
  170.     /**
  171.     * Enables or disables the recursive operation.
  172.     *
  173.     * @param  boolean
  174.     * @access public
  175.     */
  176.     function setRecursiveOperation($recursiveOperation) {
  177.         if (is_bool($recursiveOperation)) {
  178.             $this->_recursiveOperation = $recursiveOperation;
  179.         }
  180.     }
  181.  
  182.     // }}}
  183.     // {{{ function function getLock($namespace)
  184.  
  185.     /**
  186.     * Lock all namespace handlers except a given one.
  187.     *
  188.     * @string namespace
  189.     * @return boolean
  190.     * @access public
  191.     * @see    releaseLock()
  192.     */
  193.     function getLock($namespace) {
  194.         if (!$this->_locked) {
  195.             $namespacePrefixes = array_keys($this->overloadedNamespaces);
  196.  
  197.             foreach ($namespacePrefixes as $namespacePrefix) {
  198.                 if ($namespacePrefix != $namespace) {
  199.                     unset($this->overloadedNamespaces[$namespacePrefix]['active']);
  200.                 }
  201.             }
  202.  
  203.             $this->_locked = true;
  204.  
  205.             return true;
  206.         }
  207.  
  208.         return false;
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ function releaseLock()
  213.  
  214.     /**
  215.     * Releases a lock.
  216.     *
  217.     * @access public
  218.     * @see    getLock()
  219.     */
  220.     function releaseLock() {
  221.         $namespacePrefixes = array_keys($this->overloadedNamespaces);
  222.  
  223.         foreach ($namespacePrefixes as $namespacePrefix) {
  224.             $this->overloadedNamespaces[$namespacePrefix]['active'] = true;
  225.         }
  226.  
  227.         $this->_locked = false;
  228.     }
  229.  
  230.     // }}}
  231. }
  232.  
  233. /*
  234.  * vim600:  et sw=2 ts=2 fdm=marker
  235.  * vim<600: et sw=2 ts=2
  236.  */
  237. ?>
  238.